package bug
{
	import flash.events.MouseEvent;
	
	import utils.pool.ObjectPool;

	public class Controler
	{
		private var _scene:Scene;
		private var _castle:Vector.<Castle> = new Vector.<Castle>();
		private var _ver:Vector.<Arrows> = new Vector.<Arrows>();
		private var _power:int=PowerEnum.ME;
		public function Controler(scene:Scene)
		{
			reset(scene);
		}
		
		public function reset(scene:Scene):void{
			_castle=new Vector.<Castle>();
			_ver = new Vector.<Arrows>();
			_scene = scene;
			_scene.addEventListener(MouseEvent.MOUSE_DOWN,onDown,false,0,true);
		    
		}
		
		public function dispose():void{
			_scene.removeEventListener(MouseEvent.MOUSE_DOWN,onDown);
		}
		
		private function onDown(e:MouseEvent):void{
		    var a:Castle = 	e.target as Castle;
		    if(a==null) return;
			if(a._power!=_power) return;
			if(_castle.indexOf(a)<0){
				_castle.push(a);
			}
			
			_scene.addEventListener(MouseEvent.MOUSE_MOVE,onMove,false,0,true);
			_scene.addEventListener(MouseEvent.MOUSE_UP,onUp,false,0,true);
		}
		
		private function onUp(e:MouseEvent):void{
			//var a:Castle = e.target as Castle;
			if(_target!=null){
				_scene.moveAnt(list);
			}
			_scene.removeEventListener(MouseEvent.MOUSE_MOVE,onMove);
			_scene.removeEventListener(MouseEvent.MOUSE_UP,onUp);
			
			_castle = new Vector.<Castle>();
			list=[];
			clear();	
		}
		
		private function clear():void{
			for (var j:int = 0; j < _ver.length; j++) 
			{
				//_ver[j].parent.removeChild( _ver[j]);
				_pool.addObject(_ver[j]);//回收
			}
			
			_ver  = new Vector.<Arrows>();
		}
		private var _pool:ObjectPool = new ObjectPool(Arrows);
		private function draw():void{
			clear()
			var ui:Arrows;
			var i:int;
			for ( i = 0; i < _castle.length; i++) 
			{
				ui =  _pool.getObject();//new UI_Arr();
				_ver.push(ui);
				ui.x=_castle[i].x;
				ui.y=_castle[i].y;
				ui.width = Math.sqrt(Math.pow(ui.x-_scene.mouseX,2)+Math.pow(ui.y-_scene.mouseY,2));
				ui.rotation=Math.atan2((ui.y-_scene.mouseY),(ui.x-_scene.mouseX))*(180/Math.PI);
				_scene.addChild(ui);
			}
		}
		
		private function drawT(array:Array):void{
			clear();
			var ui:Arrows;
			for (var i:int = 0; i < array.length; i++) 
			{
				ui =   _pool.getObject();//TODO:优化点
				_ver.push(ui);
				ui.x=array[i][0].x;
				ui.y=array[i][0].y;
				ui.width = Math.sqrt(Math.pow(ui.x-array[i][1].x,2)+Math.pow(ui.y-array[i][1].y,2));
				ui.rotation=Math.atan2((ui.y-array[i][1].y),(ui.x-array[i][1].x))*(180/Math.PI);
				_scene.addChild(ui);
			}
		}
		
		private function formatData(target:Castle):Array{
			var targets:Vector.<Castle> = new Vector.<Castle>();	
			var runList:Array=[];
			var _caslteCopy:Vector.<Castle> = _castle.concat();
			targets.push(target);
			_caslteCopy.splice(_caslteCopy.indexOf(target),1);
			while(targets.length>0 && _caslteCopy.length>0){
				for (var i2:int = 0; i2 < targets.length; i2++) 
				{
					var temp:Vector.<Castle> = new Vector.<Castle>();
					for (var k:int = 0; k < _caslteCopy.length; k++) 
					{
						if(targets[i2]==_caslteCopy[k]) continue;
						if(targets[i2].isBorder(_caslteCopy[k])){
							runList.push([_caslteCopy[k],targets[i2]]);
							temp.push(_caslteCopy[k]);
							_caslteCopy.splice(k,1);
							k--;
						}
					}
				}
				targets=temp;
			}
			return runList;
		}
		private var list:Array=[];
		private var _target:Castle;
		private function onMove(e:MouseEvent):void{
			//var a:Castle;// = e.target as Castle;
			_target=null ;
			var cts:Vector.<Castle> = _scene._castle;
			
			var minDis:Number=30;
			var distance:Number;
			for (var j:int = 0; j < cts.length; j++) 
			{
				 distance = Math.sqrt(Math.pow(_scene.mouseX-cts[j].x,2)+Math.pow(_scene.mouseY-cts[j].y,2));
				 if(minDis>distance){
				     minDis = distance;
					 _target=cts[j];
				 }
			}
			
			/////////
			if(_castle.length>0 && _castle[_castle.length-1]._power!=_power){
				_castle.pop();
			}
			if(_target==null){
				draw();
				return ;
			}
			
			if(_power!=_target._power){
				for (var i:int = 0; i < _castle.length; i++) 
				{
					 if(_castle[i].isBorder(_target)) break;
				}
				if(i==_castle.length){
					clear();
					return;
				}
			}
			if(_castle.indexOf(_target)<0){
				_castle.push(_target);
			}
			list=formatData(_target)
			drawT(list);
			trace(_castle.length);
		}
	}
}
import flash.display.Sprite;

import utils.pool.IRecyclable;

class Arrows extends UI_Arr implements IRecyclable{
	private var initW:Number;
	private var initH:Number;
	function Arrows():void{
	    initW=this.width;
		initH=this.height;
	}
    public function dispose():void{
	   if(parent){
	      parent.removeChild(this);
	   }
	   this.rotation=0;
	   this.width=initW;
	   this.height=initH
		
	}
	
}